package com.hackaton.discounts;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.TabHost;
import android.widget.Toast;

public class Discounts extends TabActivity {
    /** Called when the activity is first created. */
	DatabaseManager db;
	DatabaseManagerCategories dbCategories;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setData();
        setCategoriesData();
        Resources res = getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec tabSpec;
        Intent intent;
        
        intent = new Intent().setClass(this, LatestActivity.class);
        tabSpec = tabHost.newTabSpec("top")
        				 .setIndicator("Latest", res.getDrawable(R.drawable.tab_latest))
        				 .setContent(intent);
        tabHost.addTab(tabSpec);
        
        intent = new Intent().setClass(this, CategoriesActivity.class);
        tabSpec = tabHost.newTabSpec("categories")
		 				 .setIndicator("Categories", res.getDrawable(R.drawable.tab_categories))
		 				 .setContent(intent);
        tabHost.addTab(tabSpec);
        
        intent = new Intent().setClass(this, FavoritesActivity.class);
        tabSpec = tabHost.newTabSpec("favorites")
						 .setIndicator("Favorites", res.getDrawable(R.drawable.tab_favorites))
						 .setContent(intent);
        tabHost.addTab(tabSpec);
        
        tabHost.setCurrentTab(0);
    }
    
    public void setData() {
    	
    	db = new DatabaseManager(this);
    	Log.e("Count", " "+db.getAllRowsAsArrays().size());
    	if (db.getAllRowsAsArrays().size() == 0) {
    		Log.e("Array made", "true");
	    	 ArrayAdapter<CharSequence> discountText = ArrayAdapter.createFromResource(
	                 this, R.array.discount_short, android.R.layout.simple_spinner_item);
	    	 ArrayAdapter<CharSequence> discountTitle = ArrayAdapter.createFromResource(
	                 this, R.array.discount_title, android.R.layout.simple_spinner_item);
	    	 ArrayAdapter<CharSequence> discountCategory = ArrayAdapter.createFromResource(
	                 this, R.array.discount_category, android.R.layout.simple_spinner_item);
	    	 ArrayAdapter<CharSequence> discountPercentage = ArrayAdapter.createFromResource(
	                 this, R.array.discount_percentage, android.R.layout.simple_spinner_item);
	    	 ArrayAdapter<CharSequence> discountPrice = ArrayAdapter.createFromResource(
	                 this, R.array.discount_price, android.R.layout.simple_spinner_item);
	    	 
	    	 for (int i=0; i< discountText.getCount(); i++) {
	    		 db.addRow(	discountCategory.getItem(i).toString(), 
	    				 	discountTitle.getItem(i).toString(), 
	    				 	discountText.getItem(i).toString(), 
	    				 	"discount_"+(i+1),
	    				 	discountPercentage.getItem(i).toString(),
	    				 	discountPrice.getItem(i).toString()
	    		);
	    	 }
    	 }
    }
    public void setCategoriesData() {
    	
    	dbCategories = new DatabaseManagerCategories(this);
    	Log.e("Count", " "+dbCategories.getAllRowsAsArrays().size());
    	if (dbCategories.getAllRowsAsArrays().size() == 0) {
    		Log.e("Array made", "true");
	    	 ArrayAdapter<CharSequence> category = ArrayAdapter.createFromResource(
	                 this, R.array.categories, android.R.layout.simple_spinner_item);
	    	 for (int i=0; i< category.getCount(); i++) {
	    		 dbCategories.addRow(category.getItem(i).toString());
	    	 }
    	 }
    }
    
       
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_menu, menu);
	    return true;
	}
    
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.settings:
	    	Toast.makeText(Discounts.this, "settings", 4000).show();	    		    	
	    	return true;
	    case R.id.about:	
	    	Toast.makeText(Discounts.this, "about", 4000).show();	    	
	    	return true;
	    case R.id.quit:
	        finish();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
    
    
}